/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package etf.pia.controlers;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import lombok.Getter;
import lombok.Setter;
import etf.pia.entity.*;
import etf.pia.util.*;
import static etf.pia.util.MyLogger.log;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.DualListModel;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author Alejandro
 */
@Getter
@Setter
@ManagedBean(name = "profBean")
@SessionScoped
public class ProfBean implements Serializable {

    @ManagedProperty("#{loginMediator.currUser.zaposleni}")
    private Zaposleni prof;

    private StreamedContent image = null;

    private DualListModel<Zaposleni> zList;
    private ArrayList<String> pType = new ArrayList<String>();
    private ArrayList<String> oKategorija = new ArrayList<String>();
    private ArrayList<Predmet> pList;
    private Set<Obavestenja> selectedInfoList;
    private Set<Spiskovi> selectedListList;
    private ArrayList<String> tipMaterijala = new ArrayList<String>();
    private ArrayList<String> tipPredmeta = new ArrayList<String>();
    private ArrayList<String> tipObavestenja = new ArrayList<String>();
    private ArrayList<Fajlovi> predavanja = new ArrayList<Fajlovi>();
    private ArrayList<Fajlovi> vezbe = new ArrayList<Fajlovi>();
    private ArrayList<Fajlovi> rokovi = new ArrayList<Fajlovi>();
    private ArrayList<Fajlovi> labVezbeFajlovi = new ArrayList<Fajlovi>();
    private ArrayList<Fajlovi> domaciFajlovi = new ArrayList<Fajlovi>();
    private String sTipMaterijala;
    private String sTipPredmeta;
    private String sTipObavestenja;
    private Predmet selectedP = null;
    private LabVezbe selectedLab = null;
    private Domaci selectedDomaci = null;
    private Spiskovi selectedL = null;
    private Obavestenja selectedO = null;
    private String selectedType;
    private String fileType;
    private String sNaziv;
    private String sMesto;
    private String sTermin;
    private int sLimit = 0;
    private String pNaziv;
    private String pFile;
    private String pOpis;
    private String oNaslov;
    private String oTekst;
    private String sKategorija;
    private UploadedFile file;
    private UploadedFile pic;

    public ProfBean() {
        //       prof = user.getCurrUser().getZaposleni();
        log.info("ProfBean konstruktor");

        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            Criteria c = ses.createCriteria(Zaposleni.class);
            zList = new DualListModel<Zaposleni>((ArrayList<Zaposleni>) c.list(), new ArrayList<Zaposleni>());
            // prof = userBean.getCurrUser().getZaposleni();

            t.commit();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
        tipMaterijala.add("predavanja");
        tipMaterijala.add("vezbe");
        tipMaterijala.add("rokovi");

        tipPredmeta.add("obavezni");
        tipPredmeta.add("izborni");
        
        oKategorija.add("Vesti");
        oKategorija.add("Praksa");
        oKategorija.add("Posao");
        oKategorija.add("Seminar");
    }

    @PostConstruct
    public void init() {
        log.info("ProfBean konstruktor");

        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            Criteria c = ses.createCriteria(Zaposleni.class);
            zList = new DualListModel<Zaposleni>((ArrayList<Zaposleni>) c.list(), new ArrayList<Zaposleni>());
            pList = Initializer.toList(prof);

//        image = new DefaultStreamedContent(new ByteArrayInputStream(prof.getSlika()));
            selectedP = pList.get(0);

            initFileList();
//        selectedDomaci = selectedP.getDomacis().iterator().next();

            postaviO();
            postaviL();
            t.commit();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void updateCourse() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            ses.update(selectedP);
            t.commit();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }

    }

    public void saveProf() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            prof.setSlika(pic.getContents());
            ses.update(prof);
            t.commit();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void savePublication() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            Publikacije p = new Publikacije();
            p.setOpis(pOpis);
            p.setNaziv(pNaziv);
            p.setZaposlenis(new HashSet<Zaposleni>(zList.getTarget()));
            p.setData(file.getContents());
            p.setTip("tip");
            ses.save(p);
            t.commit();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void addInfo() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            Obavestenja o = new Obavestenja();
            o.setNaslov(oNaslov);
            o.setArhiviran(Byte.MIN_VALUE);
            o.setDatum(new Date());
            o.setTekst(oTekst);
            o.setKategorija(sKategorija);
            o.setPredmet(selectedP);
            ses.save(o);
            t.commit();

            selectedP.getObavestenjas().add(o);
            oNaslov = null;
            oTekst = null;
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void updateInfo() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            ses.update(selectedO);
            t.commit();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void updateList() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            ses.update(selectedL);
            t.commit();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void addList() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            Spiskovi s = new Spiskovi();
            s.setNaziv(sNaziv);
            s.setTermin(sTermin);
            s.setMesto(sMesto);
            s.setPredmet(selectedP);
            s.setOgranicenje(sLimit);
            s.setZakljucan(false);

            ses.save(s);
            selectedP.getSpiskovis().add(s);
            t.commit();

            oNaslov = null;
            oTekst = null;
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void saveLab() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            ses.save(selectedLab);
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void saveDomaci() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            ses.save(selectedDomaci);
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public ArrayList<Object> toList(Set<Object> o) {
        if (o == null) {
            return null;
        }
        return new ArrayList<Object>(o);
    }

    public void postaviO() {
        selectedInfoList = selectedP.getObavestenjas();
        if (!selectedInfoList.isEmpty()) {
            selectedO = selectedInfoList.iterator().next();
        } else {
            selectedO = null;
        }
    }

    public void postaviL() {
        selectedListList = selectedP.getSpiskovis();
        if (!selectedListList.isEmpty()) {
            selectedL = selectedListList.iterator().next();
        } else {
            selectedL = null;
        }
    }

    public void initFileList() {
        predavanja.clear();
        vezbe.clear();
        rokovi.clear();
        for (Fajlovi m : new ArrayList<Fajlovi>(selectedP.getFajlovis())) {
            String tip = m.getTip();
            if (tip.equals("predavanje")) {
                predavanja.add(m);
            } else if (tip.equals("vezbe")) {
                vezbe.add(m);
            } else if (tip.equals("rokovi")) {
                rokovi.add(m);
            }
        }
        selectedLab = selectedP.getLabVezbes().iterator().next();
        selectedDomaci = selectedP.getDomacis().iterator().next();

        initLabFile();
        initDomaciFile();
    }

    public void initLabFile() {
        labVezbeFajlovi.clear();
        if (!selectedLab.getFajlovis().isEmpty()) {
            labVezbeFajlovi.addAll(selectedLab.getFajlovis());
        }
    }

    public void initDomaciFile() {
        domaciFajlovi.clear();
        if (!selectedDomaci.getFajlovis().isEmpty()) {
            domaciFajlovi.addAll(selectedDomaci.getFajlovis());
        }
    }

    public void handleFileUploadPredavanja(FileUploadEvent event) {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            Fajlovi f = new Fajlovi();
            UploadedFile uf = event.getFile();
            f.setPredmet(selectedP);
            f.setTip("predavanje");
            f.setData(uf.getContents());
            f.setNaziv(uf.getFileName());
            f.setVelicina(uf.getSize());
            f.setDatum(new Date());
            ses.save(f);
            t.commit();

            predavanja.add(f);
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void handleFileUploadVezbe(FileUploadEvent event) {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            Fajlovi f = new Fajlovi();
            UploadedFile uf = event.getFile();
            f.setPredmet(selectedP);
            f.setTip("vezbe");
            f.setData(uf.getContents());
            f.setNaziv(uf.getFileName());
            f.setVelicina(uf.getSize());
            f.setDatum(new Date());
            ses.save(f);
            t.commit();

            vezbe.add(f);
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void handleFileUploadRokovi(FileUploadEvent event) {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            Fajlovi f = new Fajlovi();
            UploadedFile uf = event.getFile();
            f.setPredmet(selectedP);
            f.setTip("rokovi");
            f.setData(uf.getContents());
            f.setNaziv(uf.getFileName());
            f.setVelicina(uf.getSize());
            f.setDatum(new Date());
            ses.save(f);
            t.commit();

            rokovi.add(f);
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void handleFileUploadLab(FileUploadEvent event) {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            Fajlovi f = new Fajlovi();
            UploadedFile uf = event.getFile();
            f.setPredmet(selectedP);
            f.setTip("labVezbe");
            f.setData(uf.getContents());
            f.setNaziv(uf.getFileName());
            f.setVelicina(uf.getSize());
            f.setDatum(new Date());
            f.getLabVezbes().add(selectedLab);
            ses.save(f);
            selectedLab.getFajlovis().add(f);
            t.commit();

            labVezbeFajlovi.add(f);
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void handleFileUploadDomaci(FileUploadEvent event) {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            Fajlovi f = new Fajlovi();
            UploadedFile uf = event.getFile();
            f.setPredmet(selectedP);
            f.setTip("domaci");
            f.setData(uf.getContents());
            f.setNaziv(uf.getFileName());
            f.setVelicina(uf.getSize());
            f.setDatum(new Date());
            f.getDomacis().add(selectedDomaci);
            ses.save(f);
            selectedDomaci.getFajlovis().add(f);
            t.commit();

            domaciFajlovi.add(f);
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void deleteF(Fajlovi f) {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            ses.delete(f);
            t.commit();
            if (f.getTip().equals("predavanje")) {
                predavanja.remove(f);
            } else if (f.getTip().equals("vezbe")) {
                vezbe.remove(f);
            } else if (f.getTip().equals("rokovi")) {
                rokovi.remove(f);
            } else if (f.getTip().equals("domaci")) {
                domaciFajlovi.remove(f);
            } else if (f.getTip().equals("labVezbe")) {
                labVezbeFajlovi.remove(f);
            }
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }
}
